package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qf.mapper.DtsGoodsProductMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.pojo.DtsGoodsProduct;
import com.qf.service.DtsGoodsProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class DtsGoodsProductServiceImpl implements DtsGoodsProductService {
    @Autowired
    DtsGoodsProductMapper dtsGoodsProductMapper;
    @Override
    public Integer findCount() {
        Integer count = dtsGoodsProductMapper.selectCount(null);
        return count;
    }

    @Override
    public void updateProduct(Integer id, DtsGoodsProduct[] products) {
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId,id);
        dtsGoodsProductMapper.delete(qw);

            if(products!=null&&products.length>0){
                for (DtsGoodsProduct product : products) {
                    product.setAddTime(new Date());
                    product.setGoodsId(id);
                    product.setUpdateTime(new Date());
                    dtsGoodsProductMapper.insert(product);
                }
            }

    }

    @Override
    public void deleteById(Integer goodsId) {

        DtsGoodsProduct product = new DtsGoodsProduct();
        product.setDeleted(true);
        LambdaQueryWrapper<DtsGoodsProduct> qw = new LambdaQueryWrapper<>();
        qw.eq(DtsGoodsProduct::getGoodsId,goodsId);

        dtsGoodsProductMapper.update(product,qw);
    }

    @Override
    public void addproduct(Integer id, DtsGoodsProduct[] products) {
        if(products!=null&&products.length>0){
            for (DtsGoodsProduct product : products) {
                product.setAddTime(new Date());
                product.setUpdateTime(new Date());
                product.setGoodsId(id);
                dtsGoodsProductMapper.insert(product);
            }
        }
    }
}
